package com.eric.nioclient.service.packet;

import java.nio.charset.Charset;

import io.netty.buffer.ByteBuf;

/**
 * Created by eric on 2017/11/16.
 */
public class ChatToGroupPacket extends Packet {
    public String user ;// 用户
    public String room ;// 房间号
    public String msg ;// 消息

    public ChatToGroupPacket(String user,String room,String msg){
        this.user = user;
        this.room = room;
        this.msg = msg ;
    }
    public ChatToGroupPacket(){}

    public void writePacketMsg(ByteBuf buf) {
        writeUTF8(buf,user);
        writeUTF8(buf,room);
        writeUTF8(buf,msg);
    }

    public void readFromBuff(ByteBuf buf) {
        user = readUTF8(buf);
        room = readUTF8(buf);
        msg = readUTF8(buf);
    }

    public PacketType getPacketType() {
        return PacketType.ChatToGroup;
    }

    public void execPacket() {

    }

    @Override
    protected int getBodyLength() {
        int length = 0 ;
        byte[] bb = user.getBytes(Charset.forName("utf-8"));
        length += bb.length + 4;
        bb = room.getBytes(Charset.forName("utf-8"));
        length += bb.length + 4;
        bb = msg.getBytes(Charset.forName("utf-8"));
        length += bb.length + 4;

        return length;
    }

    public static ChatToGroupPacket obtain(String user,String room,String msg){
        return new ChatToGroupPacket(user,room,msg);
    }

    @Override
    public Packet obtain(Object... args) {
        return new ChatToGroupPacket(args[0].toString(),args[1].toString(),args[2].toString());
    }
}
